{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 巩固题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 编写一个函数is_multiple(n,m)，用来接收两个整数值n和m，如果n是m的倍数，即存在整数i使得n=mi，那么函数返回TRUE，否则返回False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T10:52:02.168783Z",
     "start_time": "2019-05-15T10:52:02.164792Z"
    }
   },
   "outputs": [],
   "source": [
    "def is_multiple(n, m):\n",
    "    if n % m == 0:\n",
    "        return True\n",
    "    else:\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T10:52:03.912998Z",
     "start_time": "2019-05-15T10:52:03.909010Z"
    }
   },
   "outputs": [],
   "source": [
    "a = is_multiple(10, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T10:52:04.679904Z",
     "start_time": "2019-05-15T10:52:04.675917Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 编写一个Python函数is_even(k)，用来接收一个整数k，如果k是偶数返回True，否则返回False。但是，函数中不能使用乘法、除法或取余操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T11:36:30.731224Z",
     "start_time": "2019-05-15T11:36:30.726241Z"
    }
   },
   "outputs": [],
   "source": [
    "def is_even(k):\n",
    "    if k > 0:\n",
    "        while k >= 0:\n",
    "            if k == 1:\n",
    "                return False\n",
    "            elif k == 0:\n",
    "                return True\n",
    "            else:\n",
    "                k -= 2\n",
    "            \n",
    "    elif k < 0:\n",
    "        while k <= 0:\n",
    "            if k == -1:\n",
    "                return False\n",
    "            elif k == 0:\n",
    "                return True\n",
    "            else:\n",
    "                k += 2\n",
    "    else:\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T11:37:01.651731Z",
     "start_time": "2019-05-15T11:37:01.646783Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(is_even(0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 编写一个Python函数minmax(data)，用来在数的序列中找出最大数和最小数，并以一个长度为2的元组的形式返回。注意：不能通过内置函数min和max来实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T11:54:51.827159Z",
     "start_time": "2019-05-15T11:54:51.822212Z"
    }
   },
   "outputs": [],
   "source": [
    "def minmax(data):\n",
    "    min_num = data[0]\n",
    "    max_num = data[0]\n",
    "    for i in data:\n",
    "        min_num = min_num if min_num < i else i\n",
    "        max_num = max_num if max_num > i else i \n",
    "    result = (max_num, min_num)\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T11:56:07.274250Z",
     "start_time": "2019-05-15T11:56:07.270221Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(14, 0)\n"
     ]
    }
   ],
   "source": [
    "data = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]\n",
    "print(minmax(data))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 编写一个函数，用来接收正整数n，返回1~n的平方和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T12:04:00.315378Z",
     "start_time": "2019-05-15T12:04:00.312348Z"
    }
   },
   "outputs": [],
   "source": [
    "def square_sum(n):\n",
    "    result = 0\n",
    "    for i in range(1, n+1):\n",
    "        result += i**2\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T12:04:01.225429Z",
     "start_time": "2019-05-15T12:04:01.221441Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30\n"
     ]
    }
   ],
   "source": [
    "print(square_sum(4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基于Python的解析语法和内置函数sum，写一个单独的命令来计算练习1.4中的和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T12:05:28.353388Z",
     "start_time": "2019-05-15T12:05:28.350396Z"
    }
   },
   "outputs": [],
   "source": [
    "n = 4\n",
    "square_sum_single = sum(i**2 for i in range(1, n+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T12:05:36.722803Z",
     "start_time": "2019-05-15T12:05:36.717818Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30\n"
     ]
    }
   ],
   "source": [
    "print(square_sum_single)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 编写一个Python函数，用来接收正整数n，并返回1~n中所有奇数的平方和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T12:09:43.290664Z",
     "start_time": "2019-05-15T12:09:43.286636Z"
    }
   },
   "outputs": [],
   "source": [
    "def square_sum_odd(n):\n",
    "    result = sum(i**2 for i in range(1, n+1, 2))\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T12:10:09.794071Z",
     "start_time": "2019-05-15T12:10:09.790082Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "35\n"
     ]
    }
   ],
   "source": [
    "print(square_sum_odd(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T12:11:04.962780Z",
     "start_time": "2019-05-15T12:11:04.958792Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    }
   ],
   "source": [
    "print(square_sum_odd(4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基于Python的解析语法和内置函数sum，写一个单独的命令来计算练习1.6中的和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T12:14:12.627973Z",
     "start_time": "2019-05-15T12:14:12.623982Z"
    }
   },
   "outputs": [],
   "source": [
    "n = 5\n",
    "square_sum_odd_single = sum(i**2 for i in range(1, n+1, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T12:14:20.667420Z",
     "start_time": "2019-05-15T12:14:20.662433Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "35\n"
     ]
    }
   ],
   "source": [
    "print(square_sum_odd_single)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python允许负整数作为序列的索引值，比如一个长度为n的字符串是s，当索引值-n <= k < 0时，所指的元素为s[k]，那么求一个正整数索引值j>=0，使得s[j]指向的也是相同的元素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T12:46:56.252356Z",
     "start_time": "2019-05-15T12:46:56.247368Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "k=-11, j=0\n",
      "k=-10, j=1\n",
      "k=-9, j=2\n",
      "k=-8, j=3\n",
      "k=-7, j=4\n",
      "k=-6, j=5\n",
      "k=-5, j=6\n",
      "k=-4, j=7\n",
      "k=-3, j=8\n",
      "k=-2, j=9\n",
      "k=-1, j=10\n"
     ]
    }
   ],
   "source": [
    "s = 'abcdefghijk'\n",
    "for k in range(-len(s), 0):\n",
    "    for j in range(len(s)):\n",
    "        if s[k] == s[j]:\n",
    "            print('k=%d, j=%d' %(k, j) )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 要生成一个值为50,60,70,80的排列，求range构造函数的参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T12:49:31.074693Z",
     "start_time": "2019-05-15T12:49:31.070705Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "50\n",
      "60\n",
      "70\n",
      "80\n"
     ]
    }
   ],
   "source": [
    "for list in range(50, 90, 10):\n",
    "    print(list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 与1.9类似，在这里不去赘述了"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 演示怎样使用Python列表解析语法来产生列表[1,2,4,8,16,32,64,128,256]。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T12:54:19.556417Z",
     "start_time": "2019-05-15T12:54:19.551426Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 4, 8, 16, 32, 64, 128, 256]\n"
     ]
    }
   ],
   "source": [
    "print([2**i for i in range(9)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python的random模块包括一个函数choice(data)，可以从一个非空序列返回一个随机元素。Random模块还包括一个更基本的randrange函数，参数化类似于内置的range函数，可以在给定范围内返回一个随机数。只使用randrange函数，实现自己的choice函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T13:04:16.604538Z",
     "start_time": "2019-05-15T13:04:16.600548Z"
    }
   },
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T13:04:36.227775Z",
     "start_time": "2019-05-15T13:04:36.223779Z"
    }
   },
   "outputs": [],
   "source": [
    "def mychoice(data):\n",
    "    random_num = data[random.randrange(len(data)+1)]\n",
    "    return random_num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T13:04:37.180577Z",
     "start_time": "2019-05-15T13:04:37.176589Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14\n"
     ]
    }
   ],
   "source": [
    "data = [1,2,3,4,7,8,9,10,14,15,12,17,18,24]\n",
    "print(mychoice(data))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 创新题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## (1.13)编写一个函数的伪代码描述，该函数用来逆置n个整数的列表，使这些数以相反的顺序输出，并将该方法与可以实现相同功能的Python函数进行比较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T13:16:34.363429Z",
     "start_time": "2019-05-15T13:16:34.359439Z"
    }
   },
   "outputs": [],
   "source": [
    "def myreverse(mylist):\n",
    "    reversed_list = mylist[::-1]\n",
    "    return reversed_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T13:16:35.427389Z",
     "start_time": "2019-05-15T13:16:35.423398Z"
    }
   },
   "outputs": [],
   "source": [
    "test_list = [1,2,3,4,5,6,7,8,9,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T13:17:27.667960Z",
     "start_time": "2019-05-15T13:17:27.663962Z"
    }
   },
   "outputs": [],
   "source": [
    "first = sorted(test_list, reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T13:17:45.866739Z",
     "start_time": "2019-05-15T13:17:45.863747Z"
    }
   },
   "outputs": [],
   "source": [
    "second = myreverse(test_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我自己写的函数比系统的sorted函数要快"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## (1.14)编写一个Python函数，用来接收一个整数序列，并判断该序列中是否存在一对乘积是奇数的互不相同的数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T13:22:22.319088Z",
     "start_time": "2019-05-15T13:22:22.314101Z"
    }
   },
   "outputs": [],
   "source": [
    "def odd_multi_test(data):\n",
    "    a = False\n",
    "    for i in data:\n",
    "        if i % 2 != 0:\n",
    "            a = True\n",
    "            break\n",
    "    return a    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T13:23:38.911761Z",
     "start_time": "2019-05-15T13:23:38.906775Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "test_list = [2,4,3,6,8,20,44,66,88]\n",
    "print(odd_multi_test(test_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## (1.15)编写一个Python函数，用来接收一个数字序列，并判断是否所有数字都互相不同(即它们是不同的)。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T13:32:00.214426Z",
     "start_time": "2019-05-15T13:32:00.208448Z"
    }
   },
   "outputs": [],
   "source": [
    "def find_no_same(data):\n",
    "    a = True\n",
    "    length = len(data)\n",
    "    for i in range(length):\n",
    "        for k in range(length):\n",
    "            if data[k] == data[i] & k != i:\n",
    "                a = False\n",
    "                break\n",
    "    return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T13:32:34.748891Z",
     "start_time": "2019-05-15T13:32:34.744901Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "test_list = [1,2,3,7,4,5,6,7]\n",
    "print(find_no_same(test_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## (1.18)演示如何使用Python列表解析语法来产生列表[0,2,6,12,20,30,42,56,72,90]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T13:37:50.993293Z",
     "start_time": "2019-05-15T13:37:50.989262Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 2, 6, 12, 20, 30, 42, 56, 72, 90]\n"
     ]
    }
   ],
   "source": [
    "print([i*(i+1) for i in range(10)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T13:38:42.103384Z",
     "start_time": "2019-05-15T13:38:42.099396Z"
    }
   },
   "source": [
    "## (1.19)演示如何使用Python列表解析语法在不输入所有26个英文字母的情况下产生列表['a','b','c',...,'z']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-15T14:11:31.323054Z",
     "start_time": "2019-05-15T14:11:31.316078Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']\n"
     ]
    }
   ],
   "source": [
    "print([chr(i) for i in range(97, 123)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## (1.20)Python的random模块包括一个函数shuffle(data),它可以接收一个元素的列表和一个随机的重新排列元素，以使每个可能的序列发生概率相等。random模块还包括一个更基本的函数randint(a,b)，它可以返回一个从a到b(包括两个端点)的随机整数。只使用randint函数，实现自己的shuffle函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-16T06:09:40.695627Z",
     "start_time": "2019-05-16T06:09:40.687618Z"
    }
   },
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-16T06:25:47.340687Z",
     "start_time": "2019-05-16T06:25:47.328652Z"
    }
   },
   "outputs": [],
   "source": [
    "def myshuffle(data):\n",
    "    length = len(data)\n",
    "    output = []\n",
    "    list_table = []\n",
    "    for i in range(length):\n",
    "        while True:\n",
    "            a = random.randint(0, length-1)\n",
    "            if a not in list_table: break\n",
    "        list_table.append(a)\n",
    "        output.append(data[a])\n",
    "    return output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-16T06:25:48.486563Z",
     "start_time": "2019-05-16T06:25:48.478476Z"
    }
   },
   "outputs": [],
   "source": [
    "test_list = [1,2,3,4,5,6,7,8,9,0]\n",
    "result = myshuffle(test_list)\n",
    "random.shuffle(test_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-16T06:25:50.145210Z",
     "start_time": "2019-05-16T06:25:50.141173Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 2, 3, 1, 7, 4, 0, 6, 9, 8]\n",
      "[0, 4, 8, 9, 1, 2, 5, 3, 7, 6]\n"
     ]
    }
   ],
   "source": [
    "print(result)\n",
    "print(test_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## (1.22)编写一个Python程序，用来接收长度为n的两个整型数组a和b并返回数组a和b的点积。也就是返回一个长度为n的数组c，即c[i]=a[i]*b[i],for i = 0,...,n-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-16T06:46:46.442452Z",
     "start_time": "2019-05-16T06:46:46.434485Z"
    }
   },
   "outputs": [],
   "source": [
    "def list_multi(a,b):\n",
    "    n = len(a)\n",
    "    c = []\n",
    "    for i in range(n):\n",
    "        c.append(a[i] * b[i])\n",
    "    return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-16T06:46:47.330488Z",
     "start_time": "2019-05-16T06:46:47.322490Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 12, 21, 32]\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "b = [5,6,7,8]\n",
    "print(list_multi(a,b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## (1.24)编写一个Python函数，计算所给字符串中元音字母的个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-16T06:59:01.239264Z",
     "start_time": "2019-05-16T06:59:01.231267Z"
    }
   },
   "outputs": [],
   "source": [
    "def character(a):\n",
    "    find_table = ['a','e','u','o','i','A','E','O','U','I']\n",
    "    number = 0\n",
    "    for i in a:\n",
    "        if i in find_table:\n",
    "            number += 1\n",
    "    return number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-16T06:59:49.612081Z",
     "start_time": "2019-05-16T06:59:49.604061Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "test_list = 'I love you!'\n",
    "print(character(test_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## (1.25)编写一个Python函数，接收一个表示一个句子的字符串s，然后返回该字符串的删除了所有标点符号的副本。例如，给定字符串“Let's try, Mike.”，这个函数将返回\"Lets try Mike\"。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-16T07:23:59.900727Z",
     "start_time": "2019-05-16T07:23:59.892723Z"
    }
   },
   "outputs": [],
   "source": [
    "def nomark(s):\n",
    "    result = []\n",
    "    for i in s:\n",
    "        asc_mark = ord(i)\n",
    "        # print(asc_mark)\n",
    "        if  asc_mark == 32 or 65 <= asc_mark <= 90 or 97 <= asc_mark <= 122:\n",
    "            result += i\n",
    "            # print(i)\n",
    "    result = ''.join(result)\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-16T07:24:00.424753Z",
     "start_time": "2019-05-16T07:24:00.416735Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Holy shit \n"
     ]
    }
   ],
   "source": [
    "test_list = \"Holy shit! \"\n",
    "print(nomark(test_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## (1.26) 编写一个程序，需要从控制台输入3个整数a，b和c，并确定他们是否可以在一个正确的算术公式（在给定的顺序）下成立，如\"a+b=c\"或\"a-b=c\"或\"a*b=c\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-16T07:31:42.990563Z",
     "start_time": "2019-05-16T07:31:33.782889Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a = 10\n",
      "b = 2\n",
      "c = 8\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "a = int(input('a = '))\n",
    "b = int(input('b = '))\n",
    "c = int(input('c = '))\n",
    "if a+b==c or a-b==c or a*b==c:\n",
    "    print('True')\n",
    "else:\n",
    "    print('False')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## (1.27)在1.8节中，我们对于计算所给整数的因子时提供了3种不同的生成器的实现方法。1.8节末尾处的第三种方法是最有效的，但我们注意到，它没有按照递增顺序来产生因子。修改生成器，使得其按递增顺序来产生因子，同时保持其性能优势。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-16T10:09:04.900639Z",
     "start_time": "2019-05-16T10:09:04.897685Z"
    }
   },
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-16T10:09:06.045169Z",
     "start_time": "2019-05-16T10:09:06.041155Z"
    }
   },
   "outputs": [],
   "source": [
    "def factors(n):\n",
    "    k = 1\n",
    "    while k * k < n:\n",
    "        if n % k == 0:\n",
    "            yield k\n",
    "            yield n//k\n",
    "        k += 1\n",
    "    if k * k == n:\n",
    "        yield k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-16T10:09:08.137340Z",
     "start_time": "2019-05-16T10:09:08.130400Z"
    }
   },
   "outputs": [],
   "source": [
    "def myfactors(n):\n",
    "    k = 1\n",
    "    mylist = []\n",
    "    while k * k < n:\n",
    "        if n % k == 0:\n",
    "            yield k \n",
    "            # yield n//k\n",
    "            mylist.append(n//k)\n",
    "        k += 1\n",
    "    if k * k == n:\n",
    "        yield k\n",
    "    # print(mylist)\n",
    "    mylist.reverse()\n",
    "    # print(mylist)\n",
    "    for factor in mylist:\n",
    "        yield factor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-16T10:12:11.255713Z",
     "start_time": "2019-05-16T10:12:11.249729Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 0 ns\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[1, 100, 2, 50, 4, 25, 5, 20, 10]"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = 100\n",
    "%time list(factors(n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-16T10:11:54.383677Z",
     "start_time": "2019-05-16T10:11:54.378612Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[100, 50, 25, 20]\n",
      "[20, 25, 50, 100]\n",
      "Wall time: 997 µs\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[1, 2, 4, 5, 10, 20, 25, 50, 100]"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = 100\n",
    "%time list(myfactors(m))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## (1.28)题目太长，不打了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-16T10:36:38.454681Z",
     "start_time": "2019-05-16T10:36:38.450716Z"
    }
   },
   "outputs": [],
   "source": [
    "def norm(v,p):\n",
    "    a = 0\n",
    "    for i in v:\n",
    "        a += i**2\n",
    "    normalization = pow(a, 1/p)\n",
    "    return normalization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-16T10:41:25.261555Z",
     "start_time": "2019-05-16T10:41:25.257528Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.0\n"
     ]
    }
   ],
   "source": [
    "test_list = [4, 3]\n",
    "print(norm(test_list,2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 项目"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## (1.29)编写一个Python程序，输出由字母'c','a','t','d','o','g'组成的所有可能的字符串(每个字母只能使用一次)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-16T16:04:57.120410Z",
     "start_time": "2019-05-16T16:04:57.117452Z"
    }
   },
   "outputs": [],
   "source": [
    "import itertools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-16T16:08:02.062871Z",
     "start_time": "2019-05-16T16:08:02.047913Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "catdog\n",
      "catdgo\n",
      "catodg\n",
      "catogd\n",
      "catgdo\n",
      "catgod\n",
      "cadtog\n",
      "cadtgo\n",
      "cadotg\n",
      "cadogt\n",
      "cadgto\n",
      "cadgot\n",
      "caotdg\n",
      "caotgd\n",
      "caodtg\n",
      "caodgt\n",
      "caogtd\n",
      "caogdt\n",
      "cagtdo\n",
      "cagtod\n",
      "cagdto\n",
      "cagdot\n",
      "cagotd\n",
      "cagodt\n",
      "ctadog\n",
      "ctadgo\n",
      "ctaodg\n",
      "ctaogd\n",
      "ctagdo\n",
      "ctagod\n",
      "ctdaog\n",
      "ctdago\n",
      "ctdoag\n",
      "ctdoga\n",
      "ctdgao\n",
      "ctdgoa\n",
      "ctoadg\n",
      "ctoagd\n",
      "ctodag\n",
      "ctodga\n",
      "ctogad\n",
      "ctogda\n",
      "ctgado\n",
      "ctgaod\n",
      "ctgdao\n",
      "ctgdoa\n",
      "ctgoad\n",
      "ctgoda\n",
      "cdatog\n",
      "cdatgo\n",
      "cdaotg\n",
      "cdaogt\n",
      "cdagto\n",
      "cdagot\n",
      "cdtaog\n",
      "cdtago\n",
      "cdtoag\n",
      "cdtoga\n",
      "cdtgao\n",
      "cdtgoa\n",
      "cdoatg\n",
      "cdoagt\n",
      "cdotag\n",
      "cdotga\n",
      "cdogat\n",
      "cdogta\n",
      "cdgato\n",
      "cdgaot\n",
      "cdgtao\n",
      "cdgtoa\n",
      "cdgoat\n",
      "cdgota\n",
      "coatdg\n",
      "coatgd\n",
      "coadtg\n",
      "coadgt\n",
      "coagtd\n",
      "coagdt\n",
      "cotadg\n",
      "cotagd\n",
      "cotdag\n",
      "cotdga\n",
      "cotgad\n",
      "cotgda\n",
      "codatg\n",
      "codagt\n",
      "codtag\n",
      "codtga\n",
      "codgat\n",
      "codgta\n",
      "cogatd\n",
      "cogadt\n",
      "cogtad\n",
      "cogtda\n",
      "cogdat\n",
      "cogdta\n",
      "cgatdo\n",
      "cgatod\n",
      "cgadto\n",
      "cgadot\n",
      "cgaotd\n",
      "cgaodt\n",
      "cgtado\n",
      "cgtaod\n",
      "cgtdao\n",
      "cgtdoa\n",
      "cgtoad\n",
      "cgtoda\n",
      "cgdato\n",
      "cgdaot\n",
      "cgdtao\n",
      "cgdtoa\n",
      "cgdoat\n",
      "cgdota\n",
      "cgoatd\n",
      "cgoadt\n",
      "cgotad\n",
      "cgotda\n",
      "cgodat\n",
      "cgodta\n",
      "actdog\n",
      "actdgo\n",
      "actodg\n",
      "actogd\n",
      "actgdo\n",
      "actgod\n",
      "acdtog\n",
      "acdtgo\n",
      "acdotg\n",
      "acdogt\n",
      "acdgto\n",
      "acdgot\n",
      "acotdg\n",
      "acotgd\n",
      "acodtg\n",
      "acodgt\n",
      "acogtd\n",
      "acogdt\n",
      "acgtdo\n",
      "acgtod\n",
      "acgdto\n",
      "acgdot\n",
      "acgotd\n",
      "acgodt\n",
      "atcdog\n",
      "atcdgo\n",
      "atcodg\n",
      "atcogd\n",
      "atcgdo\n",
      "atcgod\n",
      "atdcog\n",
      "atdcgo\n",
      "atdocg\n",
      "atdogc\n",
      "atdgco\n",
      "atdgoc\n",
      "atocdg\n",
      "atocgd\n",
      "atodcg\n",
      "atodgc\n",
      "atogcd\n",
      "atogdc\n",
      "atgcdo\n",
      "atgcod\n",
      "atgdco\n",
      "atgdoc\n",
      "atgocd\n",
      "atgodc\n",
      "adctog\n",
      "adctgo\n",
      "adcotg\n",
      "adcogt\n",
      "adcgto\n",
      "adcgot\n",
      "adtcog\n",
      "adtcgo\n",
      "adtocg\n",
      "adtogc\n",
      "adtgco\n",
      "adtgoc\n",
      "adoctg\n",
      "adocgt\n",
      "adotcg\n",
      "adotgc\n",
      "adogct\n",
      "adogtc\n",
      "adgcto\n",
      "adgcot\n",
      "adgtco\n",
      "adgtoc\n",
      "adgoct\n",
      "adgotc\n",
      "aoctdg\n",
      "aoctgd\n",
      "aocdtg\n",
      "aocdgt\n",
      "aocgtd\n",
      "aocgdt\n",
      "aotcdg\n",
      "aotcgd\n",
      "aotdcg\n",
      "aotdgc\n",
      "aotgcd\n",
      "aotgdc\n",
      "aodctg\n",
      "aodcgt\n",
      "aodtcg\n",
      "aodtgc\n",
      "aodgct\n",
      "aodgtc\n",
      "aogctd\n",
      "aogcdt\n",
      "aogtcd\n",
      "aogtdc\n",
      "aogdct\n",
      "aogdtc\n",
      "agctdo\n",
      "agctod\n",
      "agcdto\n",
      "agcdot\n",
      "agcotd\n",
      "agcodt\n",
      "agtcdo\n",
      "agtcod\n",
      "agtdco\n",
      "agtdoc\n",
      "agtocd\n",
      "agtodc\n",
      "agdcto\n",
      "agdcot\n",
      "agdtco\n",
      "agdtoc\n",
      "agdoct\n",
      "agdotc\n",
      "agoctd\n",
      "agocdt\n",
      "agotcd\n",
      "agotdc\n",
      "agodct\n",
      "agodtc\n",
      "tcadog\n",
      "tcadgo\n",
      "tcaodg\n",
      "tcaogd\n",
      "tcagdo\n",
      "tcagod\n",
      "tcdaog\n",
      "tcdago\n",
      "tcdoag\n",
      "tcdoga\n",
      "tcdgao\n",
      "tcdgoa\n",
      "tcoadg\n",
      "tcoagd\n",
      "tcodag\n",
      "tcodga\n",
      "tcogad\n",
      "tcogda\n",
      "tcgado\n",
      "tcgaod\n",
      "tcgdao\n",
      "tcgdoa\n",
      "tcgoad\n",
      "tcgoda\n",
      "tacdog\n",
      "tacdgo\n",
      "tacodg\n",
      "tacogd\n",
      "tacgdo\n",
      "tacgod\n",
      "tadcog\n",
      "tadcgo\n",
      "tadocg\n",
      "tadogc\n",
      "tadgco\n",
      "tadgoc\n",
      "taocdg\n",
      "taocgd\n",
      "taodcg\n",
      "taodgc\n",
      "taogcd\n",
      "taogdc\n",
      "tagcdo\n",
      "tagcod\n",
      "tagdco\n",
      "tagdoc\n",
      "tagocd\n",
      "tagodc\n",
      "tdcaog\n",
      "tdcago\n",
      "tdcoag\n",
      "tdcoga\n",
      "tdcgao\n",
      "tdcgoa\n",
      "tdacog\n",
      "tdacgo\n",
      "tdaocg\n",
      "tdaogc\n",
      "tdagco\n",
      "tdagoc\n",
      "tdocag\n",
      "tdocga\n",
      "tdoacg\n",
      "tdoagc\n",
      "tdogca\n",
      "tdogac\n",
      "tdgcao\n",
      "tdgcoa\n",
      "tdgaco\n",
      "tdgaoc\n",
      "tdgoca\n",
      "tdgoac\n",
      "tocadg\n",
      "tocagd\n",
      "tocdag\n",
      "tocdga\n",
      "tocgad\n",
      "tocgda\n",
      "toacdg\n",
      "toacgd\n",
      "toadcg\n",
      "toadgc\n",
      "toagcd\n",
      "toagdc\n",
      "todcag\n",
      "todcga\n",
      "todacg\n",
      "todagc\n",
      "todgca\n",
      "todgac\n",
      "togcad\n",
      "togcda\n",
      "togacd\n",
      "togadc\n",
      "togdca\n",
      "togdac\n",
      "tgcado\n",
      "tgcaod\n",
      "tgcdao\n",
      "tgcdoa\n",
      "tgcoad\n",
      "tgcoda\n",
      "tgacdo\n",
      "tgacod\n",
      "tgadco\n",
      "tgadoc\n",
      "tgaocd\n",
      "tgaodc\n",
      "tgdcao\n",
      "tgdcoa\n",
      "tgdaco\n",
      "tgdaoc\n",
      "tgdoca\n",
      "tgdoac\n",
      "tgocad\n",
      "tgocda\n",
      "tgoacd\n",
      "tgoadc\n",
      "tgodca\n",
      "tgodac\n",
      "dcatog\n",
      "dcatgo\n",
      "dcaotg\n",
      "dcaogt\n",
      "dcagto\n",
      "dcagot\n",
      "dctaog\n",
      "dctago\n",
      "dctoag\n",
      "dctoga\n",
      "dctgao\n",
      "dctgoa\n",
      "dcoatg\n",
      "dcoagt\n",
      "dcotag\n",
      "dcotga\n",
      "dcogat\n",
      "dcogta\n",
      "dcgato\n",
      "dcgaot\n",
      "dcgtao\n",
      "dcgtoa\n",
      "dcgoat\n",
      "dcgota\n",
      "dactog\n",
      "dactgo\n",
      "dacotg\n",
      "dacogt\n",
      "dacgto\n",
      "dacgot\n",
      "datcog\n",
      "datcgo\n",
      "datocg\n",
      "datogc\n",
      "datgco\n",
      "datgoc\n",
      "daoctg\n",
      "daocgt\n",
      "daotcg\n",
      "daotgc\n",
      "daogct\n",
      "daogtc\n",
      "dagcto\n",
      "dagcot\n",
      "dagtco\n",
      "dagtoc\n",
      "dagoct\n",
      "dagotc\n",
      "dtcaog\n",
      "dtcago\n",
      "dtcoag\n",
      "dtcoga\n",
      "dtcgao\n",
      "dtcgoa\n",
      "dtacog\n",
      "dtacgo\n",
      "dtaocg\n",
      "dtaogc\n",
      "dtagco\n",
      "dtagoc\n",
      "dtocag\n",
      "dtocga\n",
      "dtoacg\n",
      "dtoagc\n",
      "dtogca\n",
      "dtogac\n",
      "dtgcao\n",
      "dtgcoa\n",
      "dtgaco\n",
      "dtgaoc\n",
      "dtgoca\n",
      "dtgoac\n",
      "docatg\n",
      "docagt\n",
      "doctag\n",
      "doctga\n",
      "docgat\n",
      "docgta\n",
      "doactg\n",
      "doacgt\n",
      "doatcg\n",
      "doatgc\n",
      "doagct\n",
      "doagtc\n",
      "dotcag\n",
      "dotcga\n",
      "dotacg\n",
      "dotagc\n",
      "dotgca\n",
      "dotgac\n",
      "dogcat\n",
      "dogcta\n",
      "dogact\n",
      "dogatc\n",
      "dogtca\n",
      "dogtac\n",
      "dgcato\n",
      "dgcaot\n",
      "dgctao\n",
      "dgctoa\n",
      "dgcoat\n",
      "dgcota\n",
      "dgacto\n",
      "dgacot\n",
      "dgatco\n",
      "dgatoc\n",
      "dgaoct\n",
      "dgaotc\n",
      "dgtcao\n",
      "dgtcoa\n",
      "dgtaco\n",
      "dgtaoc\n",
      "dgtoca\n",
      "dgtoac\n",
      "dgocat\n",
      "dgocta\n",
      "dgoact\n",
      "dgoatc\n",
      "dgotca\n",
      "dgotac\n",
      "ocatdg\n",
      "ocatgd\n",
      "ocadtg\n",
      "ocadgt\n",
      "ocagtd\n",
      "ocagdt\n",
      "octadg\n",
      "octagd\n",
      "octdag\n",
      "octdga\n",
      "octgad\n",
      "octgda\n",
      "ocdatg\n",
      "ocdagt\n",
      "ocdtag\n",
      "ocdtga\n",
      "ocdgat\n",
      "ocdgta\n",
      "ocgatd\n",
      "ocgadt\n",
      "ocgtad\n",
      "ocgtda\n",
      "ocgdat\n",
      "ocgdta\n",
      "oactdg\n",
      "oactgd\n",
      "oacdtg\n",
      "oacdgt\n",
      "oacgtd\n",
      "oacgdt\n",
      "oatcdg\n",
      "oatcgd\n",
      "oatdcg\n",
      "oatdgc\n",
      "oatgcd\n",
      "oatgdc\n",
      "oadctg\n",
      "oadcgt\n",
      "oadtcg\n",
      "oadtgc\n",
      "oadgct\n",
      "oadgtc\n",
      "oagctd\n",
      "oagcdt\n",
      "oagtcd\n",
      "oagtdc\n",
      "oagdct\n",
      "oagdtc\n",
      "otcadg\n",
      "otcagd\n",
      "otcdag\n",
      "otcdga\n",
      "otcgad\n",
      "otcgda\n",
      "otacdg\n",
      "otacgd\n",
      "otadcg\n",
      "otadgc\n",
      "otagcd\n",
      "otagdc\n",
      "otdcag\n",
      "otdcga\n",
      "otdacg\n",
      "otdagc\n",
      "otdgca\n",
      "otdgac\n",
      "otgcad\n",
      "otgcda\n",
      "otgacd\n",
      "otgadc\n",
      "otgdca\n",
      "otgdac\n",
      "odcatg\n",
      "odcagt\n",
      "odctag\n",
      "odctga\n",
      "odcgat\n",
      "odcgta\n",
      "odactg\n",
      "odacgt\n",
      "odatcg\n",
      "odatgc\n",
      "odagct\n",
      "odagtc\n",
      "odtcag\n",
      "odtcga\n",
      "odtacg\n",
      "odtagc\n",
      "odtgca\n",
      "odtgac\n",
      "odgcat\n",
      "odgcta\n",
      "odgact\n",
      "odgatc\n",
      "odgtca\n",
      "odgtac\n",
      "ogcatd\n",
      "ogcadt\n",
      "ogctad\n",
      "ogctda\n",
      "ogcdat\n",
      "ogcdta\n",
      "ogactd\n",
      "ogacdt\n",
      "ogatcd\n",
      "ogatdc\n",
      "ogadct\n",
      "ogadtc\n",
      "ogtcad\n",
      "ogtcda\n",
      "ogtacd\n",
      "ogtadc\n",
      "ogtdca\n",
      "ogtdac\n",
      "ogdcat\n",
      "ogdcta\n",
      "ogdact\n",
      "ogdatc\n",
      "ogdtca\n",
      "ogdtac\n",
      "gcatdo\n",
      "gcatod\n",
      "gcadto\n",
      "gcadot\n",
      "gcaotd\n",
      "gcaodt\n",
      "gctado\n",
      "gctaod\n",
      "gctdao\n",
      "gctdoa\n",
      "gctoad\n",
      "gctoda\n",
      "gcdato\n",
      "gcdaot\n",
      "gcdtao\n",
      "gcdtoa\n",
      "gcdoat\n",
      "gcdota\n",
      "gcoatd\n",
      "gcoadt\n",
      "gcotad\n",
      "gcotda\n",
      "gcodat\n",
      "gcodta\n",
      "gactdo\n",
      "gactod\n",
      "gacdto\n",
      "gacdot\n",
      "gacotd\n",
      "gacodt\n",
      "gatcdo\n",
      "gatcod\n",
      "gatdco\n",
      "gatdoc\n",
      "gatocd\n",
      "gatodc\n",
      "gadcto\n",
      "gadcot\n",
      "gadtco\n",
      "gadtoc\n",
      "gadoct\n",
      "gadotc\n",
      "gaoctd\n",
      "gaocdt\n",
      "gaotcd\n",
      "gaotdc\n",
      "gaodct\n",
      "gaodtc\n",
      "gtcado\n",
      "gtcaod\n",
      "gtcdao\n",
      "gtcdoa\n",
      "gtcoad\n",
      "gtcoda\n",
      "gtacdo\n",
      "gtacod\n",
      "gtadco\n",
      "gtadoc\n",
      "gtaocd\n",
      "gtaodc\n",
      "gtdcao\n",
      "gtdcoa\n",
      "gtdaco\n",
      "gtdaoc\n",
      "gtdoca\n",
      "gtdoac\n",
      "gtocad\n",
      "gtocda\n",
      "gtoacd\n",
      "gtoadc\n",
      "gtodca\n",
      "gtodac\n",
      "gdcato\n",
      "gdcaot\n",
      "gdctao\n",
      "gdctoa\n",
      "gdcoat\n",
      "gdcota\n",
      "gdacto\n",
      "gdacot\n",
      "gdatco\n",
      "gdatoc\n",
      "gdaoct\n",
      "gdaotc\n",
      "gdtcao\n",
      "gdtcoa\n",
      "gdtaco\n",
      "gdtaoc\n",
      "gdtoca\n",
      "gdtoac\n",
      "gdocat\n",
      "gdocta\n",
      "gdoact\n",
      "gdoatc\n",
      "gdotca\n",
      "gdotac\n",
      "gocatd\n",
      "gocadt\n",
      "goctad\n",
      "goctda\n",
      "gocdat\n",
      "gocdta\n",
      "goactd\n",
      "goacdt\n",
      "goatcd\n",
      "goatdc\n",
      "goadct\n",
      "goadtc\n",
      "gotcad\n",
      "gotcda\n",
      "gotacd\n",
      "gotadc\n",
      "gotdca\n",
      "gotdac\n",
      "godcat\n",
      "godcta\n",
      "godact\n",
      "godatc\n",
      "godtca\n",
      "godtac\n"
     ]
    }
   ],
   "source": [
    "string_original = ['c','a','t','d','o','g']\n",
    "for i in itertools.permutations(string_original, 6):\n",
    "    print(''.join(i))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## (1.30)编写一个Python程序，输入一个大于2的正整数，求将该数反复被2整除直到商小于2为止的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-16T16:12:09.958828Z",
     "start_time": "2019-05-16T16:12:07.461012Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Please input a positive number larger than 2: 50\n",
      "除的次数为：  5\n"
     ]
    }
   ],
   "source": [
    "n = int(input(\"Please input a positive number larger than 2: \"))\n",
    "times = 0\n",
    "while True:\n",
    "    n = n / 2\n",
    "    times += 1\n",
    "    if (n < 2): break\n",
    "print(\"除的次数为： \", times)        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## (1.31)编写一个可以“找零钱”的程序。程序应该将两个数字作为输入，一个是需要支付的钱数，另一个是你给的钱数。当你需要支付的和所给的钱数不同时，它应该返回所找的纸币和硬币的数量。纸币和硬币的值可以基于之前或现任政府的货币体系。试设计程序，以便返回尽可能少的纸币和硬币。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-17T02:27:57.210121Z",
     "start_time": "2019-05-17T02:27:46.018116Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "需要支付的钱数： 108.9\n",
      "已支付的钱数： 120.1\n",
      "应返回的十块纸币数为:  1\n",
      "应返回的一块纸币数为:  1\n",
      "应返回的一毛硬币数为:  2\n"
     ]
    }
   ],
   "source": [
    "needed_money = float(input(\"需要支付的钱数： \"))\n",
    "payed_money = float(input(\"已支付的钱数： \"))\n",
    "if payed_money >= needed_money:\n",
    "    extra_money = payed_money - needed_money\n",
    "    extra_money = round(extra_money, 1)\n",
    "    #print(extra_money)\n",
    "    hundreds = extra_money // 100\n",
    "    if hundreds != 0:\n",
    "        print(\"应返回的一百块纸币数为: \", int(hundreds))\n",
    "    extra_money -= hundreds * 100\n",
    "    extra_money = round(extra_money, 1)\n",
    "    #print(extra_money)\n",
    "    fifties = extra_money // 50\n",
    "    if fifties != 0:\n",
    "        print(\"应返回的五十块纸币数为: \", int(fifties))\n",
    "    extra_money -= fifties * 50\n",
    "    extra_money = round(extra_money, 1)\n",
    "    #print(extra_money)\n",
    "    twenties = extra_money // 20\n",
    "    if twenties != 0:\n",
    "        print(\"应返回的二十块纸币数为: \", int(twenties))\n",
    "    extra_money -= twenties * 20\n",
    "    extra_money = round(extra_money, 1)\n",
    "    #print(extra_money)\n",
    "    tens = extra_money // 10\n",
    "    if tens != 0:\n",
    "        print(\"应返回的十块纸币数为: \", int(tens))\n",
    "    extra_money -= tens * 10\n",
    "    extra_money = round(extra_money, 1)\n",
    "    #print(extra_money)\n",
    "    fives = extra_money // 5\n",
    "    if fives != 0:\n",
    "        print(\"应返回的五块纸币数为: \", int(fives))\n",
    "    extra_money -= fives * 5\n",
    "    extra_money = round(extra_money, 1)\n",
    "    #print(extra_money)\n",
    "    ones = extra_money // 1\n",
    "    if ones != 0:\n",
    "        print(\"应返回的一块纸币数为: \", int(ones))\n",
    "    extra_money -= ones\n",
    "    extra_money = round(extra_money, 1)\n",
    "    #print(extra_money)\n",
    "    beneath_one = int(extra_money * 10)\n",
    "    #print(beneath_one)\n",
    "    if beneath_one != 0:\n",
    "        print(\"应返回的一毛硬币数为: \", int(beneath_one))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## (1.35)生日悖论是说，当房间中的人数n超过23时，那么该房间里有两个人的生日相同的可能性是一半以上。这其实不是一个悖论，但许多人觉得不可思议。设计一个Python程序，可以通过一系列随机生成的生日的实验来测试这个悖论，例如可以n=5,10,15,20,...,100测试这个悖论。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-17T02:44:19.835142Z",
     "start_time": "2019-05-17T02:44:19.827143Z"
    }
   },
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-17T03:18:54.669076Z",
     "start_time": "2019-05-17T03:18:54.657039Z"
    }
   },
   "outputs": [],
   "source": [
    "def list_create(n):\n",
    "    birthday_list = []\n",
    "    for i in range(n):\n",
    "        month = random.randint(1, 12)\n",
    "        if month in [1,3,5,7,8,10,12]:\n",
    "            day = random.randint(1, 31)\n",
    "        elif month == 2:\n",
    "            day = random.randint(1, 28)\n",
    "        else:\n",
    "            day = random.randint(1, 30)\n",
    "        birthday_list.append((month, day))\n",
    "    return birthday_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-17T03:18:55.797238Z",
     "start_time": "2019-05-17T03:18:55.789238Z"
    }
   },
   "outputs": [],
   "source": [
    "def birthday_set(birth_list):\n",
    "    birth_set = set(birth_list)\n",
    "    return birth_set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-17T03:21:18.108846Z",
     "start_time": "2019-05-17T03:21:18.100845Z"
    }
   },
   "outputs": [],
   "source": [
    "def chance_test(count, n):\n",
    "    shit = 0\n",
    "    for i in range(count):\n",
    "        if len(birthday_set(list_create(n))) < n:\n",
    "            shit += 1\n",
    "    print(shit)\n",
    "    return shit/count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-17T03:23:21.468189Z",
     "start_time": "2019-05-17T03:23:18.268227Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "32\n",
      "人数为5的可能性为0.032\n",
      "106\n",
      "人数为10的可能性为0.106\n",
      "263\n",
      "人数为15的可能性为0.263\n",
      "405\n",
      "人数为20的可能性为0.405\n",
      "570\n",
      "人数为25的可能性为0.57\n",
      "731\n",
      "人数为30的可能性为0.731\n",
      "825\n",
      "人数为35的可能性为0.825\n",
      "878\n",
      "人数为40的可能性为0.878\n",
      "942\n",
      "人数为45的可能性为0.942\n",
      "965\n",
      "人数为50的可能性为0.965\n",
      "994\n",
      "人数为55的可能性为0.994\n",
      "992\n",
      "人数为60的可能性为0.992\n",
      "996\n",
      "人数为65的可能性为0.996\n",
      "1000\n",
      "人数为70的可能性为1.0\n",
      "999\n",
      "人数为75的可能性为0.999\n",
      "1000\n",
      "人数为80的可能性为1.0\n",
      "1000\n",
      "人数为85的可能性为1.0\n",
      "1000\n",
      "人数为90的可能性为1.0\n",
      "1000\n",
      "人数为95的可能性为1.0\n",
      "1000\n",
      "人数为100的可能性为1.0\n"
     ]
    }
   ],
   "source": [
    "for n in range(5, 101, 5):\n",
    "    print(\"人数为{}的可能性为{}\".format(n, chance_test(1000, n)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {
    "height": "35px",
    "width": "160px"
   },
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "position": {
    "height": "195.545px",
    "left": "791.091px",
    "right": "20px",
    "top": "120px",
    "width": "352.545px"
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
